﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using webapp4U.BOL;
using webapp4U;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>

namespace webapp4U.DAL
{
    public class RegisterMemberDAL
    {
       

        #region ObjIDataReader

        /// <summary>
        /// Returns a new Products instance filled with the DataReader's current record data 
        /// </summary>
        private RegisterMemberBOL GetMemberFromReader(IDataReader dr)
        {
            RegisterMemberBOL obj = new RegisterMemberBOL();
            obj.FullName = (dr["customer_name"] is DBNull) ? String.Empty : (String)dr["customer_name"];
            obj.NickName = (dr["NickName"] is DBNull) ? String.Empty : (String)dr["NickName"];
            obj.Email = (dr["email"] is DBNull) ? String.Empty : (String)dr["email"];
            obj.Password = (dr["Password"] is DBNull) ? String.Empty : (String)dr["Password"];
            obj.Gender = (dr["sex"] is DBNull) ? false : (bool)dr["sex"];
            obj.Birthday = (dr["Birthday"] is DBNull) ? String.Empty : (String)dr["Birthday"];
            obj.ReceiveDailyMail = (dr["ReceiveDailyEmail"] is DBNull) ? false : (bool)dr["ReceiveDailyEmail"];
            
            return obj;
        }
        #endregion

        private List<RegisterMemberBOL> GetRegisterMemberCollectionFromReader(IDataReader dr)
        {

            List<RegisterMemberBOL> obj = new List<RegisterMemberBOL>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    obj.Add(GetMemberFromReader(dr));
                }
                dr.Close();
            }
            return obj;
        }

        // Procced Order
        public DataTable dtListProvince(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.Get_Data_DataTable(Configuration.CurrentConnectionString, ref error, "TTr_GetProvince", CommandType.StoredProcedure);
        }

        public DataTable dtListTownByProvinceCode(ref string error, string ProvinceCode)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.Get_Data_DataTable(Configuration.CurrentConnectionString, ref error, "TTr_GetTownByProvinceCode", CommandType.StoredProcedure,
                 new SqlParameter("@ProvinceCode", ProvinceCode));
        }
       
       
        //public List<RegisterMemberBOL> GetMemberList(String OrderId, ref string error)
        //{

        //    ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
        //    SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_GetOrderDetailTempByOrderId",
        //        CommandType.StoredProcedure, new SqlParameter("@tranno", OrderId));
        //    return GetRegisterMemberCollectionFromReader(dr);

        //}       
               

        public bool InsertMemberRegister(ref string error, string CustomerName, string NickName, string Email,
            string Password, bool Gender, string Birthday, bool ReceiveDailyEmail)
        { 
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "TTr_InsertMemberRegister", ref error,
                new SqlParameter("@customer_name", CustomerName),
                new SqlParameter("@NickName", NickName),
                new SqlParameter("@email", Email),
                new SqlParameter("@Password", Password),
                new SqlParameter("@sex", Gender),
                new SqlParameter("@Birthday", Birthday),
                new SqlParameter("@ReceiveDailyEmail", ReceiveDailyEmail));
        }

        //kiểm tra email đã tồn tại tiên
        public bool CheckEmail(ref string error, string Email)
        {
            string strResult = "";
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            strResult = obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_CheckEmail", CommandType.StoredProcedure,
                new SqlParameter("@email", Email));
            if (strResult == "True")
                return true;
            else
                return false;
                    
        }

    }
}